package com.sylar.happyreader;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;

import android.app.Activity;
import android.app.ActivityGroup;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.sylar.happyreader.adapter.BookStyleAdapter;
import com.sylar.happyreader.bean.BookItem;
import com.sylar.happyreader.listener.Callback;
import com.sylar.happyreader.net.HttpTool;
import com.sylar.happyreader.tools.Config;
import com.sylar.happyreader.tools.TaskUtils;
import com.sylar.happyreader.tools.Utils;

public class ManBookListActivity extends ActivityGroup implements OnItemClickListener,Callable<String>, Callback<String>{
	private ListView mBookClassList;
	private String sex = "man";
	private BookStyleAdapter bookClassAdapter;
	private List<BookItem> books = new ArrayList<BookItem>();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.bookclasslist);
		
		sex = this.getIntent().getStringExtra("sex");
		
		LayoutInflater mInflater = LayoutInflater.from(this);
		View mViewheader = mInflater.inflate(R.layout.head, null);
		TextView headText = (TextView) mViewheader.findViewById(R.id.headText);
		headText.setText("分类选择");
		
		mBookClassList = (ListView) findViewById(R.id.bookclass_item_list);
	
		mBookClassList.addHeaderView(mViewheader, null, false);
		mBookClassList.setOnItemClickListener(this);
		mBookClassList.setFastScrollEnabled(true);
		
		TaskUtils.doAsync(this, null, null, this, this);
		
	}
	
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		
		if(bookClassAdapter == null) return;
		
		final String className = bookClassAdapter.getItem(position-1);
		
		TaskUtils.doAsync(this, null, null, new Callable<String>() {
			public String call() throws Exception {
				String url = Config.RequestUri + "sex=" + sex + "&style=" + Uri.encode(className);
				String httpResult = HttpTool.getString(getApplicationContext(), url);
				return httpResult;
			}
		},new Callback<String>() {
			public void onCallback(String pCallbackValue) {
				if(!TextUtils.isEmpty(pCallbackValue)){
					String[] manbooklist = pCallbackValue.split(":");
					manbooklist = Utils.SubArray(manbooklist);
					books.clear();
					for(String item:manbooklist){
						BookItem book = new BookItem(item,sex,className);
						books.add(book);
					}
					Intent i = new Intent(ManBookListActivity.this, BookGridActivity.class);
					i.putExtra("books", (Serializable)books);
					View v = getLocalActivityManager().startActivity(BookGridActivity.class.getName(), i).getDecorView();
					setContentView(v);
				}else {
					Toast.makeText(ManBookListActivity.this, "加载失败了哦", Toast.LENGTH_SHORT).show();
				}
				
			}
		});
	}

	public void onCallback(String pCallbackValue) {
		if (!TextUtils.isEmpty(pCallbackValue)) {
			String[] manList = pCallbackValue.split(":");
			manList = Utils.SubArray(manList);
			bookClassAdapter = new BookStyleAdapter(this, 0, manList);
			mBookClassList.setAdapter(bookClassAdapter);
		} else {
			Toast.makeText(this, "加载失败了哦", Toast.LENGTH_SHORT).show();
		}
	}

	public String call() throws Exception {
		String woManlists = HttpTool.getString(getApplicationContext(), Config.RequestUri + "sex="+sex);
		return woManlists;
	}
}
